package com.restkeeper.utils.wx;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 文件名: WeChatUtil
 * 描述: 微信工具类
 */
@Slf4j
public class WeChatUtil {
    public static void main(String[] args) {
        long currentSeconds = DateUtil.currentSeconds();
        System.out.println("当前时间戳是："+currentSeconds);
 
    }
 
    /**
     * 用sha-1算法验证token
     *
     * @param sign_token 与公众号中配置的签名token保持一致
     * @param timestamp
     * @param nonce
     * @return
     */
    public static String getSha1(String sign_token, String timestamp, String nonce) {
        String[] strings = new String[]{sign_token, timestamp, nonce};
        Arrays.sort(strings);
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < strings.length; i++) {
            stringBuilder.append(strings[i]);
        }
        MessageDigest md = null;
        String tmpStr = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            //将三个字符串拼到一起,进行sha1加密
            byte[] digest = md.digest(stringBuilder.toString().getBytes());
            tmpStr = byteArrToHexStr(digest);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            log.error("错误信息:{}", e.getMessage());
        }
        return tmpStr;
    }
 
    /**
     * 将字节转换成十六进制字符串
     *
     * @param b
     * @return
     */
    private static String byteToHexStr(byte b) {
        char[] chars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] tempChar = new char[2];
        tempChar[0] = chars[(b >>> 4) & 0X0F];
        tempChar[1] = chars[b & 0X0F];
        return new String(tempChar);
    }
 
    /**
     * 将字节数组转换成十六进制字符串
     *
     * @param byteArr
     * @return
     */
    private static String byteArrToHexStr(byte[] byteArr) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < byteArr.length; i++) {
            stringBuilder.append(byteToHexStr(byteArr[i]));
        }
        return stringBuilder.toString();
    }
 
    /**
     * xml转map
     *
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String, String> xmlToMap(HttpServletRequest request) throws IOException, DocumentException {
        // 将解析结果存储在HashMap中
        Map<String, String> map = new HashMap<>();
        // 从request中取得输入流
        InputStream inputStream = request.getInputStream();
        // 读取输入流
        SAXReader reader = new SAXReader();
        Document document = reader.read(inputStream);
        // 得到xml根元素
        Element element = document.getRootElement();
        // 得到根元素的所有子节点
        List<Element> list = element.elements();
        // 遍历所有子节点
        for (Element e : list) {
            map.put(e.getName(), e.getText());
        }
        // 释放资源
        inputStream.close();
        return map;
    }
}